package cn.wenhaha.common.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.wenhaha.common.utils.YamlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;


public class GlobalApp implements Serializable {

    private   String link;
    private   String host;
    private   String database;
    private   String password;
    private   Integer port;
    private   String username;
    private   String installDir;
    private   String flag;
    /** 0是mysql 1是sqllite **/
    private   Integer type;
    private static  GlobalApp globalApp;


    public static  GlobalApp getInstance(){
        return getInstance(false);
    }

    public static synchronized GlobalApp getInstance(boolean refresh){
        if (globalApp!=null && !refresh){
            return  globalApp;
        }
        if (StrUtil.isEmpty(Global.getConfig()) || !FileUtil.exist(Global.getRootPath() + Global.getConfig())){
            return null;
        }
        YamlUtils yamlUtils = new YamlUtils(Global.getRootPath()+ Global.getConfig());
        globalApp=new GlobalApp();
        globalApp.flag=(String) yamlUtils.getByKey("app.cache_flag");
        if (StrUtil.isEmpty(globalApp.flag)){
            return null;
        }
        globalApp.link=(String) yamlUtils.getByKey("datasource.link");
        globalApp.host=(String) yamlUtils.getByKey("datasource.host");
        globalApp.database=(String) yamlUtils.getByKey("datasource.database");
        if (yamlUtils.getByKey("datasource.port") instanceof  Integer){
            globalApp.port=(Integer)yamlUtils.getByKey("datasource.port");
        }
        globalApp.password=(String) yamlUtils.getByKey("datasource.password");
        globalApp.username=(String) yamlUtils.getByKey("datasource.username");
        globalApp.installDir=(String) yamlUtils.getByKey("site.install_dir");
        globalApp.type=(Integer) yamlUtils.getByKey("datasource.type");
        return globalApp;
    }

    /**
     * <p>
     *    是否已经配置过数据源
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-10-28 20:04
     */
    public static boolean isLoadConfigDb(){
        GlobalApp globalApp = GlobalApp.getInstance(false);
        if (globalApp == null || StrUtil.isEmpty(globalApp.getFlag())) {
            return false;
        }
        return  true;
    }

    public String getLink() {
        return link;
    }

    public String getHost() {
        return host;
    }

    public String getDatabase() {
        return database;
    }

    public String getPassword() {
        return password;
    }

    public Integer getPort() {
        return port;
    }

    public String getUsername() {
        return username;
    }

    public String getInstallDir() {
        return installDir;
    }

    public String getFlag() {
        return flag;
    }

    public Integer getType() {
        return type;
    }

    public static GlobalApp getGlobalApp() {
        return globalApp;
    }
}
